<?php
class carController extends appController
{
    //奖品页面
   public function prize()
   {
       $page = yf_empty($_REQUEST['page'], 1, true);
       
       $prize_name = $_REQUEST['prize_name'];
       $support = $_REQUEST['support'];
       
       $list = Model('car')->get_prize($prize_name,$support,$page);
       
       $this->assign('result',$list['data']);
       $this->assign('user_list_page',$list['pagelink']);
       $this->display('prize.html');
   }
   //增加奖品
   public function add_prize()
   {
       $this->display('add_prize.html');
   }
   //奖品时段页面
   public function rule()
   {
       $page = yf_empty($_REQUEST['page'], 1, true);
       
       $prize_name = $_REQUEST['prize_name'];
       $id = $_REQUEST['id'];
       
       $list = Model('car')->get_rule($prize_name,$id,$page);
       foreach($list['data'] as $k => $v)
       {
           $list['data'][$k]['begintime'] = date('Y-m-d H:i:s',$v['begintime']);
           $list['data'][$k]['endtime'] = date('Y-m-d H:i:s',$v['endtime']);
       }
       
       $this->assign('result',$list['data']);
       $this->assign('user_list_page',$list['pagelink']);
       $this->display('rule.html');
   }
   //增加奖品时段
   public function add_rule()
   {
       $prize_name = Model('car')->get_prize_name();
       
       $this->assign('prize_name',$prize_name);
       $this->display('add_rule.html');
   }
   //中奖用户页面
   public function win_users()
   {
       $page = $_REQUEST['page']>0?$_REQUEST['page']:1;
       
       $prize_name = $_REQUEST['prize_name'];
       
       $list = Model('car')->get_win_users($prize_name,$page);
       
       foreach($list['data'] as $k => $v)
       {
           $list['data'][$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
       }
       
       $this->assign('result',$list['data']);
       $this->assign('user_list_page',$list['pagelink']);
       $this->display('win_users.html');
   }
   //修改奖品信息
   public function update_prize()
   {
       $id = $_GET['id'];
       $res = Model('car')->get_prize_one($id);
       $this->assign("res",$res);
       $this->display('add_prize.html');
   }
   //修改奖品时段信息
   public function update_rule()
   {
       $id = $_GET['id'];
       $res = Model('car')->get_rule_one($id);
       $this->assign("res",$res);
       $prize_name = Model('car')->get_prize_name();
        
       $this->assign('prize_name',$prize_name);
       $this->display('add_rule.html');
   }
   //保存新奖品信息
   public function set_prize()
   {
       $message = array('error'=>1,'content'=>'');
       $id = $_POST['id'];
       $prize_name = $_POST['prize_name'];
       $support = $_POST['support'];
       $money = $_POST['money'];
       if(!is_numeric($money))
       {
           $message['content'] = '价格格式错误';
           die(json_encode($message));
       }
       $mark = $_POST['mark'];
       $pby = $_POST['pby'];
       if($pby<1 || $pby>100)
       {
           $message['content'] = '中奖概率填写错误';
           die(json_encode($message));
       }
       $pri = $_POST['pri'];
       if($pri<1)
       {
           $message['content'] = '中奖优先级填写错误';
           die(json_encode($message));
       }
       $card_id = $_POST['card_id'];
       
       $result = Model('car')->set_prize($id,$prize_name,$support,$money,$mark,$pby,$pri,$card_id);
       if($result)
       {
           $message['error'] = 0;
           $message['content'] = '保存成功';
           die(json_encode($message));
       }else{
           $message['content'] = '操作失败，请重试！';
           die(json_encode($message));
       }  
   }
   //删除指定记录
   public function del_prize()
   {
       $id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
	
	    if(is_array($id))
	    {
	        foreach($id as $k=>$v)
	        {
	            $result=Model('car')->del_prize($v);
	        }
	    }
	    else
	    {
	        $id=intval($id);
	
	        $result=Model('car')->del_prize($id);
	    }
	    print_r(json_encode($result));
   }
   
   //删除指定记录
   public function del_rule()
   {
       $id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
   
       if(is_array($id))
       {
           foreach($id as $k=>$v)
           {
               $result=Model('car')->del_rule($v);
           }
       }
       else
       {
           $id=intval($id);
   
           $result=Model('car')->del_rule($id);
       }
       print_r(json_encode($result));
   }
   
   //保存新奖品时段信息
   public function set_rule()
   {
       $message = array('error'=>1,'content'=>'');
       $id = $_POST['id'];
       $pid = $_POST['pid'];
       $nums = $_POST['nums'];
       if($nums < 1)
       {
           $message['content'] = '奖品数量填写错误';
           die(json_encode($message));
       }
       
       $begintime = strtotime($_POST['begintime']);
       $endtime = strtotime($_POST['endtime']);
       if($begintime > $endtime)
       {
           $message['content'] = '开始时间必须早于结束时间';
           die(json_encode($message));
       }
       
       $result = Model('car')->set_rule($id,$pid,$nums,$begintime,$endtime);
       if($result)
       {
           $message['error'] = 0;
           $message['content'] = '保存成功';
           die(json_encode($message));
       }else{
           $message['content'] = '操作失败，请重试！';
           die(json_encode($message));
       }
   }
   
   
}?>